package com.ebelter.seneo.ui.activitys;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.RegisterResponse;
import com.ebelter.btcomlib.utils.MD5Utils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.VerifyUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.seneo.R;
import com.ebelter.seneo.models.SeneoNet;


//注册
public class SeneoRegisterActivity extends SeneoBaseActivity {


    public static final String TAG = "SeneoRegisterActivity";

    EditText lg_nick_ed;
    EditText lg_email_ed;
    EditText lg_psw_ed;
    EditText rg_psw2_ed;
    ImageView lg_psw_iv;

    private boolean displayPsw;

    @Override
    protected int loadLayoutById() {
        return R.layout.activity_seneo_register;
    }


    @Override
    protected void initView() {
        lg_nick_ed=findViewById(R.id.lg_nick_ed);
        lg_email_ed=findViewById(R.id.lg_email_ed);
        lg_psw_ed=findViewById(R.id.lg_psw_ed);
        rg_psw2_ed=findViewById(R.id.rg_psw2_ed);
        lg_psw_iv=findViewById(R.id.lg_psw_iv);
    }




    public void pswdisChange(View view) {
        displayPsw=!displayPsw;
        updatePswModel();
    }

    private void updatePswModel(){
        if (displayPsw){
            //密码可见
            lg_psw_ed.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            rg_psw2_ed.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            ViewUtils.setImageResource(lg_psw_iv,R.drawable.ic_psw1);
        }else {
            //密码不可见
            lg_psw_ed.setTransformationMethod(PasswordTransformationMethod.getInstance());
            rg_psw2_ed.setTransformationMethod(PasswordTransformationMethod.getInstance());
            ViewUtils.setImageResource(lg_psw_iv,R.drawable.ic_psw2);
        }
    }

    public void login(View view) {

        if (TextUtils.equals(From,SeneoLoginActivity.TAG)){
            //说明是从登录界面跳转过来的
            finish();
        }else {
            openActivity(TAG,SeneoLoginActivity.class);
        }
    }


    //注册
    public void register(View view) {


        String name = lg_nick_ed.getText().toString().trim();
        String email = lg_email_ed.getText().toString().trim();
        String psw1 = lg_psw_ed.getText().toString().trim();
        String psw2 =rg_psw2_ed.getText().toString().trim();

      
        if (TextUtils.isEmpty(name)){
            ToastUtil.show(R.string.Nikename_cannot_be_empty);
            return;
        }


        if (name.length()>20){
            ToastUtil.show(R.string.nikename_over_tip);
            return;
        }

        if (TextUtils.isEmpty(email)){
            ToastUtil.show(R.string.Email_must_not_be_empty);
            return;
        }
        if (!VerifyUtils.emailFormat(email)){
            ToastUtil.show(R.string.Incorrect_email_format);
            return;
        }



        if (TextUtils.isEmpty(psw1)){
            ToastUtil.show(R.string.The_Password_cannot_be_empty);
            return;
        }

        if (psw1.length()!=6){
            ToastUtil.show("Type the six-digit password.");
            return;
        }


        if (!TextUtils.equals(psw1,psw2)){
            ToastUtil.show(R.string.The_password_is_inconsistency_twice);//前后密码输入不一致
            return;
        }


        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }




    }
}
